package com.example;


import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @Author 郅占伟
 * @ClassName TestDockerLog
 * @Description TODO
 * @Date 2024年09月28日
 * @Version: 1.0
 */
public class TestDockerLog {
    @Test
    public void testDockerLog() {
        String log = "{\"log\":\"2024-09-28 05:03:08.230  INFO 1 --- [      Thread-15] c.x.s.c.PLC.InputDataAllServiceForA      : 开始++++++++++++++++++++++++++++++入料\n\",\"stream\":\"stdout\",\"time\":\"2024-09-27T21:03:08.230265628Z\"}";
        DockerLog dockerLog = JSON.parseObject(log, DockerLog.class);
        System.out.println(dockerLog.toString());
    }

    @Test
    public void testDockerLog2() {
        File file = new File("src/main/resources/61b31c44069ebd4a42c57e46dafa87e9881fb7e959fa8d758db77bb05edf26af-json.log");


        try (FileReader fileReader = new FileReader(file);
             BufferedReader bufferedReader = new BufferedReader(fileReader);
        ) {
            // 判断文件是否还能读取
            while (bufferedReader.ready()) {
                String s = bufferedReader.readLine();
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void testDockerLog3() {
        File file = new File("D:\\develop\\gitCode\\java_base\\javaBaisc_introduction\\javaBasic_IO\\log\\xhh.log");

        try (FileReader fileReader = new FileReader(file);
             BufferedReader bufferedReader = new BufferedReader(fileReader);
        ) {
            // 判断文件是否还能读取
            int lineNum = 0;
            while (bufferedReader.ready()) {
                String s = bufferedReader.readLine();
                lineNum++;
                if (s.contains("com.xhh.supplier.commons.PLC.PlcController - ModbusA读出的数据")) {
                    TestIO.logFileWrite(String.valueOf(lineNum), s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 读取指定数量的日志文件，并查找包含指定关键字的日志
     */
    @Test
    public void testDockerLog4() {
        int readFileCount = 2;

        for (int i = readFileCount; i >= 1; i--) {
            System.out.println("开始读取第" + i + "个文件");
            String filePath = "";
            if (i == 1) {
                filePath = "D:\\develop\\gitCode\\java_base\\javaBaisc_introduction\\javaBasic_IO\\log\\xhh.log";
            } else {
                filePath = "D:\\develop\\gitCode\\java_base\\javaBaisc_introduction\\javaBasic_IO\\log\\xhh.log." + (i - 1);
            }
            File file = new File(filePath);
            try (FileReader fileReader = new FileReader(file);
                 BufferedReader bufferedReader = new BufferedReader(fileReader);
            ) {
                // 判断文件是否还能读取
                int lineNum = 0;
                while (bufferedReader.ready()) {
                    String s = bufferedReader.readLine();
                    lineNum++;
                    if (s.contains("Z4100023315250607002434")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "Z4100023315250607002434");
                        for (int i1 = 0; i1 < 3; i1++) {
                            TestIO.logFileWrite(String.valueOf(lineNum), bufferedReader.readLine(), "Z4100023315250607002434");
                        }
                    }

                    if (s.contains("c.x.s.commons.PLC.Service.HKSocketUtil")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "HKSocketUtil");
                        for (int i1 = 0; i1 < 3; i1++) {
                            TestIO.logFileWrite(String.valueOf(lineNum), bufferedReader.readLine(), "HKSocketUtil");
                        }
                    }

                    if (s.contains("c.x.s.b.s.o.WmsProcessingWorkOrderService")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "WmsProcessingWorkOrderService");
                    }

                    if (s.contains("com.xhh.supplier.commons.PLC.PlcController - ModbusB读出的数据")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "ModbusB");
                    }
                    if (s.contains("com.xhh.supplier.commons.PLC.PlcController - ModbusA读出的数据")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "ModbusA");
                    }
                    if (s.contains("c.x.s.commons.trayData.photoDataAll")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "photoDataAll");
                    }
                    if (s.contains("c.x.s.b.s.t.InputDataAllServiceForA")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "InputDataAllServiceForA");
                    }
                    if (s.contains("c.x.s.b.s.t.InputDataAllServiceForB")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "InputDataAllServiceForB");
                    }
                    if (s.contains("c.x.s.b.s.t.TrayDataServiceForA")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "TrayDataServiceForA");
                    }
                    if (s.contains("c.x.s.b.s.t.TrayDataServiceForB")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "TrayDataServiceForB");
                    }
                    if (s.contains("c.x.s.b.s.t.OutPutEndPhotoService")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "OutPutEndPhotoService");
                    }
                    if (s.contains("c.x.s.b.s.robotArmPickUp.RobotArmServiceForA")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "RobotArmServiceForA");
                    }
                    if (s.contains("c.x.s.b.s.robotArmPickUp.RobotArmServiceForB")) {
                        TestIO.logFileWrite(String.valueOf(lineNum), s, "RobotArmServiceForB");
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
}
